var ctx;
var cellId;
var regionId;
var buildingId ;
var unitId ;
var houseId ;
var total;
var selectCellId;
var selectRegionId;
var selectBuildingId;
var selectUnitId;
var selectHouseId;
var pSize;

function setCtx(ctx){
	this.ctx = ctx;
}


//判断是否为空
function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	
	return mess;
}

//条件搜索用户列表
function getBillList(){
	cellId = ifNull($("#cellId").val());
	regionId = ifNull($("#regionId").val());
	buildingId = ifNull($("#buildingId").val());
	unitId = ifNull($("#unitId").val());
	houseId = ifNull($("#houseId").val());
	pSize = ifNull($("#pSize").val());
	
	loadContent("/feeBill/infoList?pNo=1&cellId="+ cellId + "&regionId="+ regionId+ "&buildingId="+ 
			buildingId+ "&unitId="+ unitId+ "&houseId="+ houseId + "&pSize="+ pSize);
	
}

//跳转到指定页面
function gotoBillPage(page) {
	if(page == null){
		page = $(".inputclass").val();
	}
	cellId = ifNull($("#cellId").val());
	regionId = ifNull($("#regionId").val());
	buildingId = ifNull($("#buildingId").val());
	unitId = ifNull($("#unitId").val());
	houseId = ifNull($("#houseId").val());
	pSize = ifNull($("#pSize").val());
	
	loadContent("/feeBill/infoList?pNo="+page+"&cellId="+ cellId + "&regionId="+ regionId+ "&buildingId="+ 
			buildingId+ "&unitId="+ unitId+ "&houseId="+ houseId + "&pSize="+ pSize);
}

//跳转到上一页或者下一页
function gotoPage(page){
	cellId = ifNull($("#cellId").val());
	regionId = $("#regionId").val();
	buildingId = $("#buildingId").val();
	unitId = $("#unitId").val();
	houseId = $("#houseId").val();
	pSize = ifNull($("#pSize").val());
	
	loadContent("/feeBill/infoList?pNo="+page+"&cellId="+ cellId + "&regionId="+ regionId+ "&buildingId="+ 
			buildingId+ "&unitId="+ unitId+ "&houseId="+ houseId + "&pSize="+ pSize);
}

var id;
function changeAddr(obj,num) {
	if (num ==1) {
		id = $("#cellId").val();
		$("#selectCellId").val($("#cellId").val());
	}
	if (num ==2) {
		id = $("#regionId").val();
	}
	if (num ==3) {
		id = $("#buildingId").val();
	}
	if (num ==4) {
		id = $("#unitId").val();
	}
	if (id != ''){
		$.ajax({
	        type : "GET",
	        url : ctx + "/feeBill/changeAddr",
	        cache : false,
	        dataType : 'json',
	        data : {
	        	id : id,
	        	num:num
	        },
	        success : function(data) {
	        	if (data.success) {
	        		if (num == 1) {
	        			var result = "<option value='" + "'>区域</option>";
	                    $.each(data.obj, function(index, item) {
	                    	if(item.id == selectRegionId){
	                    		result += "<option id='" + item.name + "'" +" value='" + item.id + "'selected='selected'>" + item.name + "</option>";
	                    	}else{
	                    		result += "<option id='" + item.name + "'" +" value='" + item.id + "'>" + item.name + "</option>";
	                    	}
	                    });
	                    $("#regionId").html('');
	                    $("#regionId").append(result);
	                    $("#regionId").change();
	        		}
	        		if (num == 2) {
	        			var result = "<option value='" + "'>楼栋</option>";
	                    $.each(data.obj, function(index, item) {
	                    	if(item.id == selectBuildingId){
	                    		result += "<option id='" + item.buildingName + "'" + " value='" + item.id + "' selected='selected'>" + item.buildingName + "</option>";
	                    	}else{
	                    		result += "<option id='" + item.buildingName + "'" + " value='" + item.id + "'>" + item.buildingName + "</option>";
	                    	}
	                    });
	                    $("#buildingId").html('');
	                    $("#buildingId").append(result);
	                    $("#buildingId").change();
	        		}
	        		if (num == 3) {
	        			var result = "<option value='" + "'>单元</option>";
	                    $.each(data.obj, function(index, item) {
	                    	if(item.id == selectUnitId){
	                    		result += "<option id='" + item.name + "'" + " value='" + item.id + "' selected='selected'>" + item.name + "</option>";
	                    	}else{
	                    		result += "<option id='" + item.name + "'" + " value='" + item.id + "'>" + item.name + "</option>";
	                    	}
	                    });
	                    $("#unitId").html('');
	                    $("#unitId").append(result);
	                    $("#unitId").change();
	        		}
	        		if (num == 4) {
	        			var result = "<option value='" + "'>房间号</option>";
	                    $.each(data.obj, function(index, item) {
	                    	if(item.id == selectHouseId){
	                    		result += "<option id='" + item.name + "'" + " value='" + item.id + "' selected='selected'>" + item.name + "</option>";
	                    	}else{
	                    		result += "<option id='" + item.name + "'" + " value='" + item.id + "'>" + item.name + "</option>";
	                    	}
	                    });
	                    $("#houseId").html('');
	                    $("#houseId").append(result);
	                    $("#houseId").change();
	        		}
	            }
	        }
		});
	} else {
		if (num == 1) {
			var result = "<option value='" + "'>区域</option>";
			$("#regionId").html('');
            $("#regionId").append(result);
            $("#regionId").change();
		}
		if (num == 2) {
			var result = "<option value='" + "'>楼栋</option>";
			$("#buildingId").html('');
            $("#buildingId").append(result);
            $("#buildingId").change();
		}
		if (num == 3) {
			var result = "<option value='" + "'>单元</option>";
			$("#unitId").html('');
            $("#unitId").append(result);
            $("#unitId").change();
		}
		if (num == 4) {
			var result = "<option value='" + "'>区域</option>";
			$("#houseId").html('');
            $("#houseId").append(result);
            $("#houseId").change();
		}
	}
	
	
		
}

var num;
$(function(){
	selectCellId = $("#selectCellId").val();
	selectRegionId = $("#selectRegionId").val();
	selectBuildingId = $("#selectBuildingId").val();
	selectUnitId = $("#selectUnitId").val();
	selectHouseId = $("#selectHouseId").val();
	if(selectCellId != null){
		$.ajax({
	        type : "GET",
	        url : ctx + "/feeBill/changeAddr",
	        cache : false,
	        dataType : 'json',
	        data : {
	        	id : selectCellId,
	        	num:1
	        },
	        success : function(data) {
	        	if (data.success) {
	        			var result = "<option value='" + "'>区域</option>";
	                    $.each(data.obj, function(index, item) {
	                    	if(item.id == selectRegionId){
	                    		result += "<option id='" + item.name + "'" +" value='" + item.id + "'selected='selected'>" + item.name + "</option>";
	                    	}else{
	                    		result += "<option id='" + item.name + "'" +" value='" + item.id + "'>" + item.name + "</option>";
	                    	}
	                    });
	                    $("#regionId").html('');
	                    $("#regionId").append(result);
	                    $("#regionId").change();
	        	}
	        }
	    });
	}
})

//全选和反选
function selectAll(target){    
    if(target.checked){    
        $("#tableTbody :checkbox").attr("checked", true); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#tableTbody :checkbox").attr("checked", false); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","");
       });
    }    

}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}

//选择或取消选中当前记录
function selectOne(id,target){
    if(target.checked){    
        $("#repairInfo" + id).attr("checked", true);   
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#repairInfo" + id).attr("checked", false); 
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","");
       });
    } 
}

//删除多个id对应的记录
function deleteByIds(){
	if($("img[name='repairInfoChecked'][src*=honggou]").length == 0){
		layer.msg("请选择要删除的记录!",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}else{
		var delIds = [];
		    $("#tableTbody img[name='repairInfoChecked'][src*=honggou]").each(function(){ 
		    	delIds.push($(this).attr("class")); 
		    }); 
		    var vals = delIds.join(',');//转换为逗号隔开的字符串 
			layer.confirm('确认删除?', {icon: 3, title:'提示'}, function(index){
			    //do something
				$.post(ctx+"/feeBill/deleteInfo",{
					iDs:vals
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("删除成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							loadContent("/repairInfo/rInfoList");
						});
					} else {
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
			    layer.close(index);
			});

		
	}
}

//保存备注信息
var index;
function saveOrUpdateList(msg,url){
  //官网欢迎页
   index = layer.open({
      type: 2,
      //skin: 'layui-layer-lan',
      title: msg,
      fix: false,
      shadeClose: true,
      scrollbar: false,
      maxmin: false,
      edit: true,
      closeBtn: 1,
      area: ['500px', '323px'],
      content: url,
      btn: ['设置', '取消']
	    ,yes: function(index, layero){ //或者使用btn1
	        //按钮【按钮一】的回调
	    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
	    	iframeWin.initList();

	    },cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	    });
}

//实现字段较长时的隐藏于显示
function mouseOverInfo(item , answer){
	layer.tips(answer, '#answer' + item, {
		  tips: [3, '#2F2F2F'],
		  time: 5000
		});
}

//查看某个房间的缴费记录列表
function gotoDetail (houseId) {
	loadContent("/feeBill/gotoDetail?houseId="+houseId);
}

//查看缴费信息详情
function gotoDetailInfo (id,houseId) {
	loadContent("/feeBillDetail/gotoDetail?id="+id+"&houseId="+houseId+"&type=1");
}

//返回上一页
function cancel(){
	loadContent("/feeBill/infoList");
}

//初始化账单信息
function initList (type,id) {
	msg = "初始化账单信息";
	id = "";
	url = ctx + "/feeBill/initList?type="+type+"&houseId="+id;
	saveOrUpdateList(msg, url);
	
}

//后台缴费
function initBill (type,id) {
	url = ctx + "/feeBill/initList?type="+type+"&houseId="+id;
	if (type == 3) {
		msg = "批量导入";
		importBillList(msg, url);
	} else {
		msg = "后台缴费";
		var perPrice = $("#perPrice").val();
		if (perPrice != "" && perPrice != null && perPrice != undefined) {
			saveOrUpdateBill(msg, url);
		} else {
			layer.msg('此房屋的物业费单价为空，不能缴费!!', {
				icon : 5
			});
		}
	}
}

// 弹出缴费框
function saveOrUpdateBill(msg, url) {
		index = layer.open({
			type : 2,
			title : msg,
			fix : false,
			shadeClose : true,
			scrollbar : false,
			maxmin : false,
			edit : true,
			closeBtn : 1,
			area : [ '500px', '450px' ],
			content : url,
			btn : [ '设置', '取消' ],
			yes : function(index, layero) { // 或者使用btn1
				// 按钮【按钮一】的回调
				var iframeWin = window[layero.find('iframe')[0]['name']];
				iframeWin.saveBill();
			},
			cancel : function(index) { // 或者使用btn2
				// 按钮【按钮二】的回调
			}
		});
}

var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
// 弹出导入物业账单框
function importBillList(msg,url){
	  //官网欢迎页
	   index = layer.open({
	      type: 2,
	      //skin: 'layui-layer-lan',
	      title: msg,
	      fix: false,
	      shadeClose: true,
	      scrollbar: false,
	      maxmin: false,
	      edit: true,
	      closeBtn: 1,
	      area: ['420px', '220px'],
	      content: url,
	      btn: ['设置', '取消']
		    ,yes: function(index, layero){ //或者使用btn1
		        //按钮【按钮一】的回调
		    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
		    	iframeWin.importBill();

		    },cancel: function(index){ //或者使用btn2
		        //按钮【按钮二】的回调
		    }
		    });
	}

//导出物业费账单
function exportExcel() {
	cellId = ifNull($("#cellId").val());
	regionId = ifNull($("#regionId").val());
	buildingId = ifNull($("#buildingId").val());
	unitId = ifNull($("#unitId").val());
	houseId = ifNull($("#houseId").val());
	total = ifNull($("#total").val());
	if(total >0) {
		location.href=ctx +"/feeBill/exportFeeBillExcel?cellId="+ cellId + "&regionId="+ regionId+ "&buildingId="+ 
			buildingId+ "&unitId="+ unitId+ "&houseId="+ houseId;
	} else {
		layer.msg("未获取到数据，无法导出...",{
			icon: 2,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}
	
}

